#pragma once
#include "hashTable.h"
namespace m_std
{

    template <class K, class V, class Hash = HashFun<K>>
    class unordered_map
    {
    private:
        struct mapOfValue
        {
            const K &operator()(const pair<K, V> &data)
            {
                return data.first;
            }
        };

    public:
        typedef typename HashTable<K, pair<K, V>, Hash, mapOfValue>::iterator iterator;
        size_t size() const
        {
            return _ht.size();
        }
        size_t bucketSize() const
        {
            return _ht.bucketSize();
        }
        size_t bucketNum() const
        {
            return _ht.bucketNum();
        }
        size_t maxBucketLenth() const
        {
            return _ht.maxBucketLenth();
        }
        iterator begin()
        {
            return _ht.begin();
        }
        iterator end()
        {
            return _ht.end();
        }
        V &operator[](const K &key)
        {
            pair<iterator, bool> ret = _ht.insert(make_pair(key, V()));
            return ret.first->second;
        }
        pair<iterator, bool> insert(const pair<K, V> &data)
        {
            return _ht.insert(data);
        }

    private:
        HashTable<K, pair<K, V>, Hash, mapOfValue> _ht;
    };
    void test_map()
    {
        unordered_map<string, string> hashMap;
        hashMap.insert(make_pair("sort", "排序"));
        hashMap.insert(make_pair("string", "字符串"));
        hashMap.insert(make_pair("left", "左边"));

        unordered_map<string, string>::iterator it = hashMap.begin();
        while (it != hashMap.end())
        {
            cout << it->first << ":" << it->second << endl;
            ++it;
        }
        cout << endl;
        unordered_map<string, int> countMap;
        string arr[] = {"苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "香蕉",
                        "香蕉", "苹果", "香蕉", "西瓜"};
        for (auto e : arr)
        {
            countMap[e]++;
        }

        for (auto &kv : countMap)
        {
            cout << kv.first << ":" << kv.second << endl;
        }
    }
    void TestHT3()
    {
        int n = 29000000;
        vector<int> v;
        v.reserve(n);
        srand(time(0));
        for (int i = 0; i < n; ++i)
        {
            // v.push_back(i);
            v.push_back(rand() + i); // 重复少
                                     // v.push_back(rand());  // 重复多
        }
        size_t begin1 = clock();
        unordered_map<int, int> ht;
        for (auto e : v)
        {
            ht.insert(make_pair(e, e));
        }
        size_t end1 = clock();
        cout << "数据个数:" << ht.size() << endl;
        cout << "表的长度:" << ht.bucketSize() << endl;
        cout << "桶的个数:" << ht.bucketNum() << endl;
        cout << "平均每个桶的长度:" << (double)ht.size() / (double)ht.bucketNum() << endl;
        cout << "最长的桶的长度:" << ht.maxBucketLenth() << endl;
        cout << "负载因子:" << (double)ht.size() / (double)ht.bucketSize() << endl;
    }
};